package org.example;

import com.mysql.cj.xdevapi.Client;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

public class Server {
    private  static String PORT ;
    private  static String ADDRESS;
    public static void main(String[] args) throws IOException {
        proper();
        scheduled();

    }
    public static void proper() throws IOException {
        //读取properties
        InputStream inputStream = Client.class.getClassLoader().getResourceAsStream("server.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        //获取端口和地址
        System.out.println("port=" + properties.getProperty("port"));
        System.out.println("address=" + properties.getProperty("address"));
        System.out.println("ip=" + properties.getProperty("ip"));
        PORT = properties.getProperty("port");
        ADDRESS = properties.getProperty("address");
        inputStream .close();
    }
    public static void scheduled(){
        try {
            ServerSocket serverSocket = new ServerSocket(Integer.parseInt(PORT));
            System.out.println("服务端已启动，等待客户端连接...");

            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println("客户端连接成功！");

                // 创建线程处理每个客户端连接
                Thread thread = new Thread(() -> {
                    try {
                        InputStream inputStream = socket.getInputStream();
                        byte[] buffer = new byte[1024];
                        int bytesRead;
                        String fileName = "";

                        // 读取文件名长度
                        int fileNameLength = inputStream.read();

                        // 如果读取到的文件名长度不为-1，则继续读取文件名
                        if (fileNameLength != -1) {
                            byte[] fileNameBytes = new byte[fileNameLength];
                            inputStream.read(fileNameBytes);
                            fileName = new String(fileNameBytes);
                        }

                        FileOutputStream fileOutputStream = new FileOutputStream(ADDRESS + File.separator + fileName);
                        System.out.println(ADDRESS);
                        System.out.println(ADDRESS + fileName);
                        System.out.println(ADDRESS + File.separator + fileName);

                        // 读取文件内容并写入到文件
                        while ((bytesRead = inputStream.read(buffer)) != -1) {
                            fileOutputStream.write(buffer, 0, bytesRead);
                        }

                        fileOutputStream.close();
                        System.out.println("文件保存成功：" + fileName);

                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            socket.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

                thread.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
